# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: donggu

import datetime
import logging

from absl import app

from coin.exchange.okex.kr_rest.product import OkexProduct
import experimental.prophet as prophet
from experimental.prophet.graph import graph
from experimental.prophet.ops import timeseries

start_time = datetime.date(2019, 7, 1)
end_time = datetime.datetime(2019, 7, 1, 0, 1)


def model():
  ts = prophet.global_timestamp()

  product = OkexProduct.FromStr('BTC-USDT')
  feed = prophet.fastfeed_coin(product, 'Spot.Okex.v3')
  window = '50ms'

  # vwap deviation

  trade = prophet.trade(feed)
  vwap_deviation1 = prophet.indicator.vwap_deviation(trade, window)

  # legacy vwap
  proto = prophet.proto_from_fastfeed(feed).named_ref('proto')
  vwap_deviation2 = prophet.legacy_feature(proto,
                                           'Vwap',
                                           {
                                               'exchange': product.exchange,
                                               'product_name': product.symbol,
                                               'time_diff_min_in_ms': 50,
                                           }).vwap_deviation

  vwap_deviation1 = vwap_deviation1.named_ref('prophet')
  vwap_deviation2 = vwap_deviation2.named_ref('legacy')
  aggregator = prophet.scalar_value_aggregator([ts, vwap_deviation1, vwap_deviation2])

  return aggregator


def main(argv):
  df = prophet.run_from_fastfeed_multiprocess(model,
                                              start_time,
                                              end_time,
                                              machine='feed-01.ap-northeast-1.aws',
                                              use_run_cache=False,
                                              eval_updated_only=False)

  print(df.to_string())


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG')
  app.run(main)
